/* atomic.h : dgnsc atomic op
 * Copyright (C) 2009-2011 drangon <drangon.zhou@gmail.com>
 * 2009-06
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef INCLUDED_DGNSC_ATOMIC_H
#define INCLUDED_DGNSC_ATOMIC_H

typedef struct { volatile int value; } dgn_atomic_t;

#ifdef _WIN32

#include <windows.h>

static __inline__ int dgn_atomic_get( dgn_atomic_t * a )
{
	return a->value;
}

static __inline__ void dgn_atomic_set( dgn_atomic_t * a, int v )
{
	InterlockedExchange( (LONG *)&(a->value), v );
	return;
}

// return old value
static __inline__ int dgn_atomic_add( dgn_atomic_t * a, int v )
{
	return InterlockedExchangeAdd( (LONG *)&(a->value), v );
}

// return new value
static __inline__ int dgn_atomic_inc( dgn_atomic_t * a )
{
	return InterlockedIncrement( (LONG *)&(a->value) );
}

// return new value
static __inline__ int dgn_atomic_dec( dgn_atomic_t * a )
{
	return InterlockedDecrement( (LONG *)&(a->value) );
}

// return old val, set new value
static __inline__ int dgn_atomic_xchg( dgn_atomic_t * a, int v )
{
	return InterlockedExchange( (LONG *)&(a->value), v );
}

// return old val, compare and if equal set value
static __inline__ int dgn_atomic_cas( dgn_atomic_t * a, int v, int cmp )
{
	return InterlockedCompareExchange( (LONG *)&(a->value), v, cmp );
}

#else

// x86/amd64 only

static __inline__ int dgn_atomic_get( dgn_atomic_t * a )
{
	return a->value;
}

static __inline__ void dgn_atomic_set( dgn_atomic_t * a, int v )
{
	a->value = v;
	return;
}

// return old value
static __inline__ int dgn_atomic_add( dgn_atomic_t * a, int v )
{
	asm volatile ("lock; xaddl %0,%1"
		: "=r" (v), "=m" (a->value)
		: "0" (v), "m" (a->value)
		: "memory", "cc");
	return v;
}

// return new value
static __inline__ int dgn_atomic_inc( dgn_atomic_t * a )
{
	return dgn_atomic_add( a, 1 ) + 1;
}

// return new value
static __inline__ int dgn_atomic_dec( dgn_atomic_t * a )
{
	return dgn_atomic_add( a, -1 ) - 1;
}

// set value, return old val
static __inline__ int dgn_atomic_xchg( dgn_atomic_t * a, int v )
{
	int prev = v;
	// not need lock ??
	asm volatile ("xchgl %0, %1"
		: "=r" (prev), "+m" (a->value)
		: "0" (prev));
	return prev;
}

// compare, if equal, set value, always return old val
static __inline__ int dgn_atomic_cas( dgn_atomic_t * a, int v, int cmp )
{
	int prev;
	asm volatile ("lock; cmpxchgl %1, %2"
		: "=a" (prev)
		: "r" (v), "m" (a->value), "0"(cmp)
		: "memory", "cc");
	return prev;
}

#endif

#endif // INCLUDED_DGNSC_ATOMIC_H

